package com.example.gulimall.auth.controller;

import com.example.common.utils.R;
import com.example.common.vo.UserLoginVo;
import com.example.common.vo.UserRegisterVo;
import com.example.gulimall.auth.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>
 *
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Controller
@RequestMapping({"/auth", "/oauth2.0"})
public class AuthController {

    @Autowired
    private AuthService authService;


    @ResponseBody
    @GetMapping("/smd/sendCode")
    public R sendSms(@RequestParam("phone") String phone) {
        return this.authService.sendSms(phone);
    }


    @PostMapping("/user/register")
    public String register(@Valid UserRegisterVo vo,
                           BindingResult result,
                           RedirectAttributes attributes) {
        return this.authService.register (vo, result, attributes);
    }

    @RequestMapping("/user/login") // auth
    public String login(UserLoginVo vo, RedirectAttributes attributes){
        return this.authService.login(vo, attributes);
    }

    @GetMapping("/gitee/success")
    public String giteeSuccess (@RequestParam("code") String code, HttpSession session) {
        return this.authService.giteeSussess (code, session);
    }

    @GetMapping("/logout.html")
    public String logout (HttpSession session) {
        return this.authService.logout(session);
    }
}
